#include "VariableEntry.h"

VariableEntry::VariableEntry() : TableEntry(VARIABLE) {
	arraySize = 0;
	isFunctionParameter = false;
}

VariableEntry::VariableEntry(Type type, Attribute attrib, int arraySize, bool param) : TableEntry(VARIABLE) {
	variableType=type;
	isFunctionParameter = param;
	setAttribute(attrib, arraySize);
}

VariableEntry::VariableEntry(Type type, VariableAttribute va) : TableEntry(VARIABLE) {
	variableType=type;
	isFunctionParameter = false;
	setAttribute(va);
}

VariableEntry::~VariableEntry() { }


int VariableEntry::setAddress(int addr) {
	address = addr;
	if(variableAttribute == VA_ARRAY) {
		return addr + (4 * arraySize);
	}
	return addr + 4;
}

void VariableEntry::setAttribute(const VariableAttribute &va) {
	setAttribute(va.attrib, va.arraySize);
}

void VariableEntry::setAttribute(Attribute attrib, int aSize) {
	variableAttribute=attrib;
	arraySize=aSize;
}

Attribute VariableEntry::getAttribute() {
	return variableAttribute;
}

void VariableEntry::setParamFlag(bool isParam) {
	isFunctionParameter = isParam;
}

bool VariableEntry::isParam() const {
	return isFunctionParameter;
}

attr VariableEntry::getAttr() const {
	attr retVal;
	retVal.addr = getAddress();
	retVal.attrib = variableAttribute;
	retVal.type = variableType;
	retVal.param = isFunctionParameter;
	return retVal;
}

int VariableEntry::getArraySize() const {
	return arraySize;
}

Type VariableEntry::getType() const {
	return variableType;
}

void VariableEntry::setType(Type t) {
	if(t==T_INT || t==T_CHAR) {
		variableType=t;
	} else{
		// oops
	}
}

std::ostream& VariableEntry::inserter(std::ostream &os) const {
	switch(variableAttribute) {
	case VA_SIMPLE:
		os << "simple ";
		break;
	case VA_ARRAY:
		os << "array ";
		break;
	//case PARAMETER:
	//	os << "parameter ";
	//	break;
	}
	
	os << "variable of ";
	
	switch(variableType) {
	case T_CHAR:
		os << "type char ";
		break;
	case T_INT:
		os << "type int ";
		break;
	default:
		os << "unknown type ";
	}
	
	if(variableAttribute == VA_ARRAY && arraySize > 0) {
		os << "with " << arraySize << " elements";
	}
	
	return os;
}
